#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "../inc/admin.h"
#include "../inc/data.h"
#include "../inc/flight_node.h"
#include "../inc/general.h"
#include "../inc/login.h"
#include "../inc/myInfo.h"
#include "../inc/user_node.h"
#include "../inc/user.h"

char *getUser_type(int user_type)
{
    if (user_type == 0)
    {
        return "普通用户";
    }
    else if (user_type == 1)
    {
        return "管理员";
    }
    else if (user_type == 2)
    {
        return "超级管理员";
    }
    else
    {
        return "未知";
    }
}

void print_user(user_Node_p headNode)
{
    if (!headNode)
    {
        return;
    }

    user_Node_p currNode = headNode->next;
    printf("用户名\t  手机号\n");
    while (currNode != headNode)
    {
        if (currNode->data->user_type == 0)
        {
            printf("%s\t%s\n", currNode->data->username, currNode->data->phone);
        }
        currNode = currNode->next;
    }
}

void print_admin(user_Node_p headNode)
{
    if (!headNode)
    {
        return;
    }

    user_Node_p currNode = headNode->next;
    printf("用户名\t航空公司\n");
    while (currNode != headNode)
    {
        if (currNode->data->user_type == 1)
        {
            printf("%s\t%s\n", currNode->data->username, currNode->data->airline);
        }
        currNode = currNode->next;
    }
}

// 注册
void enroll(user_Node_p headNode)
{
    printf("                    注册                  \n");
    printf("----------------------------------------------\n");
    user_insert(headNode);
    printf("----------------------------------------------\n");
    printf("注册成功!\n");
    // Sleep(1000);
}

int getUserIndex_ByUserName(user_Node_p headNode, char *username)
{
    int index = 0;
    user_Node_p currNode = headNode->next;
    while (currNode != headNode)
    {
        if (strcmp(currNode->data->username, username) == 0)
        {
            return index;
        }
        currNode = currNode->next;
        index++;
    }
    return -1;
}

user_p scanf_enroll(user_Node_p headNode)
{
    user_p currUser = (user_p)malloc(sizeof(user_t));
    char password[20];
    printf("请输入新建账号:");
    while (1)
    {
        scanf("%19s", currUser->username);
        if (getUserIndex_ByUserName(headNode, currUser->username) == -1)
        {
            break;
        }
        else
        {
            printf("账号已存在，请重新输入:");
        }
    }

    while (1)
    {
        printf("请输入密码:");
        scanf("%19s", currUser->password);
        printf("请再次输入密码:");
        scanf("%19s", password);
        if (strcmp(currUser->password, password) == 0)
        {
            break;
        }
        else
        {
            printf("两次密码不一致，请重新输入!\n");
        }
    }
    while (1)
    {
        printf("请输入手机号:");
        scanf("%19s", currUser->phone);

        // 验证手机号长度是否为11位
        if (strlen(currUser->phone) != 11)
        {
            printf("手机号必须为11位数字,请重新输入!\n");
            continue;
        }

        // 验证手机号是否全为数字
        int valid = 1;
        for (int i = 0; i < 11; i++)
        {
            if (!isdigit(currUser->phone[i]))
            {
                valid = 0;
                break;
            }
        }

        if (!valid)
        {
            printf("手机号只能包含数字，请重新输入!\n");
            continue;
        }

        break;
    }
    currUser->user_type = 0;
    strcpy(currUser->airline, "None");
    currUser->money = 0;
    printf("用户 %s 注册成功\n", currUser->username);
    waitForAnyKey();
    return currUser;
}

// 登录
int login(user_Node_p headNode)
{
    printf("                     登录                  \n");
    printf("----------------------------------------------\n");
    user_Node_p p = headNode;
    char username[10];
    char password[10];
    printf("请输入账号:");
    scanf("%9s", username);
    printf("请输入密码:");
    scanf("%9s", password);
    int flag = verify(headNode, username, password);
    printf("----------------------------------------------\n");
    if (flag == 10 || flag == 11)
    {
        strcpy(Login_Username, username); // 登录成功，保存用户名
        printf("登录成功!正在进入系统....\n");
        cross_sleep(1000);
        return flag;
    }
    else if (flag == -1)
    {
        printf("账号不存在!\n");
        waitForAnyKey();
    }
    else if (flag == -2)
    {
        printf("密码错误!\n");
        waitForAnyKey();
    }
    return 0;
}

// 验证账号是否存在以及密码是否正确
int verify(user_Node_p headNode, char *username, char *password)
{
    user_Node_p p = headNode->next;
    while (p != headNode)
    {
        if (strcmp(p->data->username, username) == 0)
        {
            if (strcmp(p->data->password, password) == 0)
            {

                return 10 + p->data->user_type; // 返回0表示账号和密码都正确
            }
            else
            {
                return -2; // 返回1表示密码错误
            }
        }
        else
        {
            p = p->next;
        }
    }
    return -1; // 返回-1表示账号不存在
}
